package io.github.lee.core.util

import android.content.res.Resources
import android.graphics.drawable.Drawable
import android.os.Build
import androidx.annotation.ColorInt
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import androidx.core.content.res.ResourcesCompat
import androidx.fragment.app.Fragment

//
//inline fun <reified T> Fragment.getArgumentValue(key: String, defaultValue: T?): T? {
//    val args = arguments ?: return defaultValue
//
//    return when (T::class) {
//        String::class -> args.getString(key) as T
////        Boolean::class -> result as T
//        Byte::class -> args.getByte(key) as T
//        Char::class -> args.getChar(key) as T
//        Int::class -> args.getInt(key) as T
//        Short::class -> args.getShort(key) as T
//        Long::class -> args.getLong(key) as T
//        Double::class -> args.getDouble(key) as T
//        Float::class -> args.getFloat(key) as T
//        else -> throw UnsupportedOperationException("Only basic data types can be obtained")
//    }
//
//}

inline fun <reified T> Fragment.getArgumentParcelable(
    key: String,
    clazz: Class<T>
): T? {
    val args = arguments ?: return null
    return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
        args.getParcelable(key, clazz)
    } else {
        @Suppress("DEPRECATION")
        args.getParcelable(key)
    }
}


@ColorInt
fun Fragment.getColor(@ColorRes id: Int, theme: Resources.Theme? = null): Int =
    ResourcesCompat.getColor(resources, id, theme)


fun Fragment.getDrawable(@DrawableRes id: Int, theme: Resources.Theme? = null)
        : Drawable? = ResourcesCompat.getDrawable(resources, id, theme)


inline fun <reified T> Fragment.getArgumentValue(key: String, defaultValue: T?): T? {
    val bundle = arguments ?: return defaultValue
    val data = when (defaultValue) {
        is String -> bundle.getString(key, defaultValue)
        is Short -> bundle.getShort(key, defaultValue)
        is Int -> bundle.getInt(key, defaultValue)
        is Long -> bundle.getLong(key, defaultValue)
        is Float -> bundle.getFloat(key, defaultValue)
        is Double -> bundle.getDouble(key, defaultValue)
        is Byte -> bundle.getByte(key, defaultValue)
        is Char -> bundle.getChar(key, defaultValue)
        is Boolean -> bundle.getBoolean(key, defaultValue)
        else -> throw IllegalArgumentException("Unsupported data types")

    }
    return data as T


}

